package com.tavenli.services;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tavenli.entity.PersonEntity;
import com.tavenli.entity.PersonTempleteEntity;
import com.tavenli.model.PageData;
import com.tavenli.repository.PersonDao;
import com.tavenli.repository.PersonTempleteDao;

@Service
public class UPersonService {
private static Logger logger = LoggerFactory.getLogger(UCenterService.class);
	
	@Autowired
	private PersonDao personDao;
	@Autowired
	private PersonTempleteDao personTempleteDao;
	@Autowired
	private DeviceCmdService deviceCmdService;
	
	public PageData<PersonEntity> getPersonList(Integer pageIndex,
			int pageSize, String personName) {
		PageData<PersonEntity> pageData = new PageData<PersonEntity>(pageIndex, pageSize);
		Map<String, Object> paramMap = new HashMap<String, Object>();
		paramMap.put("personName", personName);
		int totalCount = personDao.queryDataCount(paramMap);
		List<PersonEntity> list = personDao.queryPageData(pageData.getStartRow(), pageSize,paramMap);
		pageData.setTotalCount(totalCount);
		pageData.setPageData(list);
		return pageData;
	}


	public PersonEntity save(PersonEntity personEntity) throws Exception {
		return (PersonEntity) this.personDao.saveAndRet(personEntity);
	}
	public PersonEntity saveAndSendToDev(PersonEntity personEntity) throws Exception {
		personEntity=(PersonEntity) this.personDao.saveAndRet(personEntity);
		//生成命令发送给设备
		String personCmdString=deviceCmdService.getPersonCmd(personEntity);
		deviceCmdService.sendCmdToDevice(personCmdString, personEntity.getPersonOldSn());
		return personEntity;
	}

	public boolean existPersonByPin(String pinString) {
		return this.personDao.existPerson(pinString);
	}


	public void update(PersonEntity personEntity) throws Exception {
		this.personDao.update(personEntity);
	}


	public PersonEntity getPersonByPin(String pinString) {
		return personDao.getPersonEntityByPin(pinString);
	}
	public PersonEntity getPersonById(int id) {
		return personDao.getPersonEntityById(id);
	}
	/**
	 * 更新或者新增用户指纹
	 * @param personTempleteEntity
	 * @throws Exception 
	 */
	@Transactional
	public void addOrUpdatePersonTemplete(PersonTempleteEntity personTempleteEntity) throws Exception {
		PersonTempleteEntity tmp=personTempleteDao.getByPersonIdAndFid(personTempleteEntity);
		if(null==tmp){
			personTempleteDao.save(personTempleteEntity);
		}else
		{
			personTempleteEntity.setId(tmp.getId());
			personTempleteDao.update(personTempleteEntity);
		}
	}
	/**
	 * 删除人员信息,顺便删除指纹，并告诉设备删除此人
	 * @since 2015年1月13日 下午3:10:29
	 * @param Id
	 * @return
	 * @throws Exception 
	 */
	public int delPerson(int id) throws Exception{
		PersonEntity personEntity=personDao.getPersonEntityById(id);
		if (null!=personEntity)
		{
			//获取指纹信息
			List<PersonTempleteEntity> listTempleteEntities=personTempleteDao.getPersonTempleteByPersonId(id);
			if (null!=listTempleteEntities)
			{
				for (int i = 0; i < listTempleteEntities.size(); i++)
				{
					//生成删除指纹命令
					String delCmdString=deviceCmdService.getDelPersonTempleteCmd(listTempleteEntities.get(i),personEntity);
					deviceCmdService.sendCmdToDevice(delCmdString,personEntity.getPersonOldSn());
					//删除数据库中的人
					personTempleteDao.delete(listTempleteEntities.get(i));
				}
			}
		}
		String delcmd=deviceCmdService.getDelPersonCmd(personEntity);
		deviceCmdService.sendCmdToDevice(delcmd,personEntity.getPersonOldSn());
		return personDao.delPerson(id);
	}
	
	
	
	
}
